const { ipcRenderer } = window.require("electron");
import { useEffect, useState } from "react";
import { history, useModel } from "umi";

export default () => {
  const [appList, setAppList] = useState<any[]>([]);
  const {
    deviceList,
    getDeviceList,
    getDeviceInfo,
    deviceInfo,
    setDeviceInfo,
  } = useModel("device");

  useEffect(() => {
    if (history.location.pathname === "/auth") {
      setAppList([]);
    }
  }, [history]);

  useEffect(() => {
    getAppList();
  }, [deviceInfo]);

  const getAppList = async () => {
    try {
      if (deviceInfo?.SN) {
        console.log("deviceInfo.SN" + deviceInfo.SN);
        const list = await ipcRenderer.invoke(
          "getAllAppListByHdc",
          deviceInfo?.SN
        );
        if (list?.length) {
          setAppList(list);
        } /*else{
                    setAppList(await ipcRenderer.invoke("getAllAppListAdb"));
                }*/
      }
    } catch (err) {
      console.log(err);
    }
  };

  return { appList, setAppList, getAppList };
};
